﻿using System;
using System.Collections.Generic;
using System.Windows.Automation;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QuickUIT.Controls;

namespace QuickUIT.Tests
{
    [TestClass]
    public class UIButtonTests
    {
        private BaseState _baseState;

        [TestInitialize]
        public void TestSetup()
        {
            _baseState = new BaseState(TestUtilities.GetTestApplicationFileName(), "@Name='QuickUIT Test Application'");
            _baseState.Execute();
        }

        [TestMethod]
        public void ShouldGetToggleState()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIButton toolbarButton = window.UIButton("@Name='A'");
            Assert.AreEqual(ToggleState.Off, toolbarButton.ToggleState);
        }

        [TestMethod]
        public void ShouldToggle()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIButton toolbarButton = window.UIButton("@Name='A'");
            Assert.AreEqual(ToggleState.Off, toolbarButton.ToggleState);
            toolbarButton.Toggle();
            Assert.AreEqual(ToggleState.On, toolbarButton.ToggleState);
        }

        [TestMethod]
        public void ShouldToggleOn()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIButton toolbarButton = window.UIButton("@Name='A'");
            Assert.AreEqual(ToggleState.Off, toolbarButton.ToggleState);
            toolbarButton.ToggleOn();
            Assert.AreEqual(ToggleState.On, toolbarButton.ToggleState);
        }

        [TestMethod]
        public void ShouldToggleOff()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIButton toolbarButton = window.UIButton("@Name='B'");
            Assert.AreEqual(ToggleState.On, toolbarButton.ToggleState);
            toolbarButton.ToggleOff();
            Assert.AreEqual(ToggleState.Off, toolbarButton.ToggleState);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))] 
        public void ShouldThrowExceptionWhenPressingToggleButton()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIButton toolbarButton = window.UIButton("@Name='C'");
            toolbarButton.Press();
        }

        [TestMethod]
        public void ShouldPressButton()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIButton button = window.UIButton("@Name='Button'");
            button.Press();
        }

        [TestMethod]
        public void ShouldClickButton()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIButton toolbarButton = window.UIButton("@Name='A'");
            Assert.AreEqual(ToggleState.Off, toolbarButton.ToggleState);
            toolbarButton.Click();
            Assert.AreEqual(ToggleState.On, toolbarButton.ToggleState);
        }

        [TestMethod]
        public void ShouldGetExpandCollapseState()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIButton button = window.UIButton("@Name='Button'");
            Assert.IsFalse(button.ExpandCollapseState.HasValue);
        }

        [TestMethod]
        public void ShouldGetAllButtons()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            List <UIButton> buttons = window.UIToolBar().UIButtons(); 
            Assert.AreEqual(4, buttons.Count);
            Assert.AreEqual("OverflowButton", buttons[0].AutomationId);
            Assert.AreEqual("A", buttons[1].Name);
            Assert.AreEqual("B", buttons[2].Name);
            Assert.AreEqual("C", buttons[3].Name);
        }

        [TestMethod]
        [ExpectedException(typeof(UIControlNotFoundException))]
        public void ShouldThrowNotFoundExceptionForNonExistentButton()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            AutomationConfiguration.Current.ObjectSearchTime = 1000;
            try
            {
                window.UIButton("[@AutomationId='NoControl']");
            }
            catch (UIControlNotFoundException ex)
            {
                Assert.AreEqual("Could not find '//UIButton[@AutomationId='NoControl']'", ex.Message);
                throw;
            }
        }

        [TestCleanup]
        public void TestTeardown()
        {
            AutomationConfiguration.Current.Reset();
            _baseState.Cleanup();
        }
    }
}